package com.run.go.gearbox.mysql.codec.parse;

import com.run.go.gearbox.mysql.io.BoundedInputStream;
import com.run.go.gearbox.mysql.response.OkResponse;
import com.run.go.gearbox.mysql.response.ServerPacket;
import io.netty.channel.Channel;

import java.io.IOException;

public abstract class ResponseStart extends DecoderState {


    @Override
    public ServerPacket parse(int length, int packetNumber, BoundedInputStream in, Channel channel) throws IOException {

        int fieldCount = in.read();
        if (fieldCount == RESPONSE_OK) {
            return OkResponse.interpretAsRegularOk(length, packetNumber, in);
        }

        if (fieldCount == RESPONSE_ERROR) {
            return decodeErrorResponse(in, length, packetNumber);
        }

        if (fieldCount == RESPONSE_EOF) {
            throw new IllegalStateException("Did not expect an EOF response from the server");
        }
        return null;
    }
    
}
